package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.Pay_DanYICZBean;
import com.kucai.yifaqapp.util.MyWebviewUtls;
import com.kucai.yifaqapp.util.SYSOUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by admin on 2018/12/4.
 */

public class onlinePay extends BaseActivitySimple {
    private EditText pay_online_one;
    private TextView pay_online_two;
    private TextView pay_online_three;
    private TextView pay_online_four;
    private Button pay_online_five;
    private List<String> mdatas = new ArrayList<>();
    Pay_DanYICZBean bean;
    RelativeLayout back;
    private String czid;
    LinearLayout skinbg;
    private TextView typename;
    private String pay_type;
    private int xiaxian = 0;
    private int shangxian = 0;
    private OptionsPickerView pvOptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pay_type = getIntent().getStringExtra("PayType");
        initview();
        initlistener();
        initData();
    }

    private void initlistener() {
        pay_online_five.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (pay_online_three.getText().toString().equals("请点击选择支付账号")) {
                        Toast.makeText(onlinePay.this, "请点击选择支付账号", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (pay_online_one.getText().toString().isEmpty()) {
                        Toast.makeText(onlinePay.this, "请输入金额", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (Double.parseDouble(pay_online_one.getText().toString()) < xiaxian) {
                        Toast.makeText(onlinePay.this, "输入金额不能少于" + xiaxian, Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (Double.parseDouble(pay_online_one.getText().toString()) > shangxian) {
                        Toast.makeText(onlinePay.this, "输入金额不能高于" + shangxian, Toast.LENGTH_SHORT).show();
                        return;
                    }
                    String url = HttpAdress.getuseAdress() + "api/m/pay/paymentSubmission" + "?id=" + czid + "&money=" + pay_online_one.getText().toString() + "&token=" + Info.user.token;
                    Intent intent = new Intent(onlinePay.this, MyWebviewUtls.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("url", url);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    finish();
                } catch (Exception e) {
                    Toast.makeText(onlinePay.this, "出现错误，请重新操作", Toast.LENGTH_SHORT).show();
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initview() {
        pay_online_one = findViewById(R.id.kj001);
        pay_online_two = findViewById(R.id.kj002);
        pay_online_three = findViewById(R.id.kj003);
        pay_online_four = findViewById(R.id.kj004);
        pay_online_five = findViewById(R.id.kj005);
        skinbg = findViewById(R.id.skinbg);
        typename = findViewById(R.id.typename);
        back = findViewById(R.id.back);
    }

    private void initData() {
        HttpJsonUtils.getHttpApiInstance()
                .getSingleCZ(HttpAdress.getuseAdress() + "api/m/user/getPay",pay_type, Info.user.token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Pay_DanYICZBean>(this) {
                    @Override
                    protected void onCustomNext(Pay_DanYICZBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                bean = resBanner;
                                for (int i = 0; i < resBanner.getRech_types().size(); i++) {
                                    mdatas.add(resBanner.getRech_types().get(i).getDsplay());
                                    typename.setText(resBanner.getRech_types().get(i).getName() + "");
                                }
                                pvOptions = new OptionsPickerView.Builder(onlinePay.this, new OptionsPickerView.OnOptionsSelectListener() {
                                    @Override
                                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                                        pay_online_two.setText("单笔下限" + bean.getRech_types().get(options1).getMin_money() + "单笔上限" + bean.getRech_types().get(options1).getMax_money());
                                        xiaxian = bean.getRech_types().get(options1).getMin_money();
                                        shangxian = bean.getRech_types().get(options1).getMax_money();
                                        pay_online_four.setText(bean.getRech_types().get(options1).getRemark() + "");
                                        czid = bean.getRech_types().get(options1).getId() + "";
                                        pay_online_three.setText(mdatas.get(options1));
                                    }
                                })
                                        .setSubCalSize(20)//确定和取消文字大小
                                        .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                                        .setCancelColor(Color.BLUE)//取消按钮文字颜色
                                        .setTextColorCenter(Color.BLACK)//设置选中项的颜色
                                        .build();
                                pvOptions.setPicker(mdatas);
                                pay_online_three.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        pvOptions.show();
                                    }
                                });

                                try {
                                    pay_online_two.setText("单笔下限" + bean.getRech_types().get(0).getMin_money() + "单笔上限" + bean.getRech_types().get(0).getMax_money());
                                    xiaxian = bean.getRech_types().get(0).getMin_money();
                                    shangxian = bean.getRech_types().get(0).getMax_money();
                                    pay_online_four.setText(bean.getRech_types().get(0).getRemark() + "");
                                    czid = bean.getRech_types().get(0).getId() + "";
                                    pay_online_three.setText(mdatas.get(0));
                                } catch (Exception e) {

                                }

                            } else {
                                Toast.makeText(onlinePay.this, resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            Toast.makeText(onlinePay.this, "异常错误，请重新操作", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }


    @Override
    protected int setContentView() {
        return R.layout.onlinepay;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }
}
